package com.dckj.auto.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.dckj.auto.common.interceptor.AuthorityAnnotation;
import com.dckj.auto.common.utils.AuthCode;
import com.dckj.auto.common.utils.JsonUtils;
import com.dckj.auto.common.utils.PageUtils;
import com.dckj.auto.entity.AuthRole;
import com.dckj.auto.service.AuthRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author WangDong123
 * @since 2018-09-03
 */
@Controller
@RequestMapping("/auth/role/")
public class AuthRoleController {

    @Autowired
    AuthRoleService roleService;


    @AuthorityAnnotation(authority = AuthCode.AUTH_ROLE_PAGE)
    @RequestMapping(value = "page")
    public String page(){
        //获取权限属性集合
        return "auth-role/list";
    }

    @RequestMapping(value = "list")
    @ResponseBody
    public JSONObject list(HttpServletRequest request,String name){
        Page<AuthRole> page = PageUtils.getPage(request);
        List<AuthRole> list = roleService.list(page, name);
        return JsonUtils.toPage(page,list);
    }

    @AuthorityAnnotation(authority = AuthCode.DELETE_AUTH_ROLE)
    @RequestMapping(value = "delete")
    @ResponseBody
    public JSONObject delete(String id){
        Integer delete = roleService.delete(id);
        if(delete>0){
            return JsonUtils.success("删除角色");
        }
        return JsonUtils.error(true,"删除角色");
    }

    @AuthorityAnnotation(authority = AuthCode.CREATE_AUTH_ROLE)
    @RequestMapping(value = "toInsert")
    public String toInsert(){
        return "auth-role/insert";
    }

    @AuthorityAnnotation(authority = AuthCode.UPDATE_AUTH_ROLE)
    @RequestMapping(value = "toUpdate")
    public String toUpdate(HttpServletRequest request, String id){
        AuthRole autoRole = roleService.selectById(id);
        request.setAttribute("role",autoRole);
        return "auth-role/update";
    }


    @AuthorityAnnotation(authority = AuthCode.CREATE_AUTH_ROLE)
    @RequestMapping(value = "doInsert")
    @ResponseBody
    public JSONObject doInsert(AuthRole role){
        Integer integer = roleService.saveOrUpdate(role);
        if(integer>0){
            return JsonUtils.success("新增角色");
        }
        return JsonUtils.error(true,"新增角色");
    }

    @AuthorityAnnotation(authority = AuthCode.UPDATE_AUTH_ROLE)
    @RequestMapping(value = "doUpdate")
    @ResponseBody
    public JSONObject doUpdate(AuthRole role){
        Integer integer = roleService.saveOrUpdate(role);
        if(integer>0){
            return JsonUtils.success("编辑角色");
        }
        return JsonUtils.error(true,"编辑角色");
    }


    @AuthorityAnnotation(authority = AuthCode.UPDATE_AUTH_ROLE_STATE)
    @RequestMapping(value = "updateState")
    @ResponseBody
    public JSONObject state(String id ,Integer state){
        String msg = state.equals(1)?"启用":"禁用";
        Integer integer = roleService.updateState(id,state);
        if(integer>0){
            return JsonUtils.success(msg+"角色");
        }
        return JsonUtils.error(true,msg+"角色");
    }




}

